﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class tab_adminDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="aid">标识列ID</param>
		public bool Exists(int aid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [tab_admin] where aid=@aid ");
			htSyn.Add("@aid", aid);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<tab_admin> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [createDate],[aid],[userName],[userPwd],[tel],[sex],[email] FROM [tab_admin]");
			 return gettab_adminBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="aid">标识列ID</param>
		/// <returns></returns>
		public tab_admin GetModel(int aid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [createDate],[aid],[userName],[userPwd],[tel],[sex],[email] FROM [tab_admin]");
			strSql.Append(" where aid=@aid ");
			htSyn.Add("@aid", aid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			tab_admin tab_adminModel = new tab_admin();
			if (dt.Rows.Count > 0)
			{
				tab_adminModel.createDate=Convert.ToDateTime(dt.Rows[0]["createDate"]);
				tab_adminModel.userName=dt.Rows[0]["userName"].ToString();
				tab_adminModel.userPwd=dt.Rows[0]["userPwd"].ToString();
				tab_adminModel.tel=dt.Rows[0]["tel"].ToString();
				tab_adminModel.sex=dt.Rows[0]["sex"].ToString();
				tab_adminModel.email=dt.Rows[0]["email"].ToString();
			}
			return tab_adminModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="tab_admin">实体</param>
		/// <returns></returns>
		public bool Update(tab_admin tab_adminModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [tab_admin] set ");
			strSql.Append("[createDate]=@createdate,");
			strSql.Append("[userName]=@username,");
			strSql.Append("[userPwd]=@userpwd,");
			strSql.Append("[tel]=@tel,");
			strSql.Append("[sex]=@sex,");
			strSql.Append("[email]=@email");

			strSql.Append(" where aid=@aid ");
			htSyn.Add("@createdate", tab_adminModel.createDate);
			htSyn.Add("@aid", tab_adminModel.aid);
			htSyn.Add("@username", tab_adminModel.userName);
			htSyn.Add("@userpwd", tab_adminModel.userPwd);
			htSyn.Add("@tel", tab_adminModel.tel);
			htSyn.Add("@sex", tab_adminModel.sex);
			htSyn.Add("@email", tab_adminModel.email);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="aid">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int aid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [tab_admin] where aid=@aid");
			htSyn.Add("aid", aid);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="tab_admin">实体</param>
		/// <returns></returns>
		public bool Insert(tab_admin tab_adminModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [tab_admin]([createDate],[userName],[userPwd],[tel],[sex],[email]) values (@createdate,@username,@userpwd,@tel,@sex,@email)");
			htSyn.Add("@createdate", tab_adminModel.createDate);
			htSyn.Add("@username", tab_adminModel.userName);
			htSyn.Add("@userpwd", tab_adminModel.userPwd);
			htSyn.Add("@tel", tab_adminModel.tel);
			htSyn.Add("@sex", tab_adminModel.sex);
			htSyn.Add("@email", tab_adminModel.email);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<tab_admin> gettab_adminBySql(string strSql)
		{
			IList<tab_admin> list = new List<tab_admin>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					tab_admin tab_adminModel = new tab_admin();
					tab_adminModel.createDate= Convert.ToDateTime(dr["createDate"]);
					tab_adminModel.aid= Convert.ToInt32(dr["aid"]);
					tab_adminModel.userName=dr["userName"].ToString();
					tab_adminModel.userPwd=dr["userPwd"].ToString();
					tab_adminModel.tel=dr["tel"].ToString();
					tab_adminModel.sex=dr["sex"].ToString();
					tab_adminModel.email=dr["email"].ToString();
					list.Add(tab_adminModel);
				}
			}
			return list;
		}
		#endregion
	}
}
